//
//  VerifyCodeViewController.m
//  insyte
//
//  Created by Vu Dang Tuan on 6/22/15.
//  Copyright (c) 2015 Robert. All rights reserved.
//

#import "VerifyCodeViewController.h"
#import "Helpers.h"
#import "UIColor+InsyteColors.h"
#import "ServiceManager.h"
#import "GlobalObject.h"
#import "ProfilesViewController.h"

@interface VerifyCodeViewController ()<UITextFieldDelegate>

@end

@implementation VerifyCodeViewController
{
    float _yAs;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
    [self setUpNavigationBar];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];
    _yAs = self.view.frame.origin.y;
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setUpNavigationBar{
    UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ic_back"] style:UIBarButtonItemStylePlain target:self action:@selector(OnBackClicked)];
    self.navigationItem.leftBarButtonItem = btnBack;
    self.navigationItem.titleView = nil;
}

- (void) OnBackClicked{
    [self.navigationController popViewControllerAnimated:YES];
}



- (void) setupView{
    [Helpers StyleButtonWithBorderColor:_btnVerify color:[UIColor loginButtonColor] corner:0];
    [Helpers StyleViewWithBorderColor:_viewCode color:[UIColor textfieldBorderColor] corner:0];
    _viewError.hidden = TRUE;
    _tfCode.delegate = self;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(dismissKeyboard)];
    
    [self.view addGestureRecognizer:tap];
}

- (IBAction)OnVerifyClicked:(id)sender {
    if ([_tfCode.text isEqualToString:@""]) {
        [_tfCode becomeFirstResponder];
        return;
    }
    [ServiceManager loginWithPhone:_tfCode.text phone:_phone cb:^(BOOL rs) {
        if (rs) {
            _viewError.hidden = TRUE;
            //
            ProfilesViewController *profile = [[ProfilesViewController alloc] init];
            UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:profile];
            [[UIApplication sharedApplication].keyWindow setRootViewController: navController];
        }else{
            _viewError.hidden = FALSE;
        }
    }];
}
- (IBAction)OnResendClicked:(id)sender {
}

#pragma keyboard

- (void)keyboardDidHide: (NSNotification *) notif{
    [Helpers scrollView:self.view y:_yAs];
}

#pragma Textfield delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [Helpers scrollView:self.view y:_yAs - 70];
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
-(void)dismissKeyboard {
    [self.tfCode resignFirstResponder];
}
@end
